/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JDialogOperacao.java
 *
 * Created on Apr 13, 2012, 7:41:30 PM
 */
package herancapessoa.gui;

import herancapessoa.controles.PessoaFisicaManager;
import herancapessoa.controles.PessoaJuridicaManager;
import herancapessoa.controles.PessoaManager;
import herancapessoa.negocio.PessoaFisica;
import herancapessoa.negocio.PessoaJuridica;
import herancapessoa.persistencia.PessoaFisicaDAO;
import herancapessoa.persistencia.PessoaJuridicaDAO;
import herancapessoa.utils.HibernateUtil;
import javax.swing.JOptionPane;
import org.hibernate.Session;

/**
 *
 * @author Ricky
 */
public class JDialogOperacao extends javax.swing.JDialog {

    private float limite;
    private float saldo;
    private PessoaJuridica pj = null;
    private PessoaFisica pf = null;
    
    /** Creates new form JDialogOperacao */
    public JDialogOperacao(java.awt.Frame parent, boolean modal,String nome) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        pj = PessoaJuridicaManager.searchByName(nome);
        if (pj != null) {
            jLabelLimite.setText("Limite: "+String.valueOf(pj.getValMaxEmp()));
            jLabelSaldo.setText("Saldo: "+String.valueOf(pj.getValEmp()));
            this.limite = pj.getValMaxEmp();
            this.saldo = pj.getValEmp();
        } else {
            pf = PessoaFisicaManager.searchByName(nome);
            jLabelLimite.setText("Limite: "+String.valueOf(pf.getValMaxEmp()));
            jLabelSaldo.setText("Saldo: "+String.valueOf(pf.getValEmp()));
            this.limite = pf.getValMaxEmp();
            this.saldo = pf.getValEmp();            
        }
        this.setResizable(false);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelEmprestimo = new javax.swing.JPanel();
        jLabelSaldo = new javax.swing.JLabel();
        jLabelLimite = new javax.swing.JLabel();
        jLabelEmp = new javax.swing.JLabel();
        jTextFieldVal = new javax.swing.JTextField();
        jButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabelSaldo.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabelSaldo.setText("jLabel1");

        jLabelLimite.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabelLimite.setText("jLabel2");

        jLabelEmp.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jLabelEmp.setText("Operação: ");

        jTextFieldVal.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        jButton.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        jButton.setText("Confirmar");
        jButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 0, 10)); // NOI18N
        jLabel1.setText("empréstimo (-) negativo");

        javax.swing.GroupLayout jPanelEmprestimoLayout = new javax.swing.GroupLayout(jPanelEmprestimo);
        jPanelEmprestimo.setLayout(jPanelEmprestimoLayout);
        jPanelEmprestimoLayout.setHorizontalGroup(
            jPanelEmprestimoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelEmprestimoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelEmprestimoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE)
                    .addComponent(jLabelSaldo)
                    .addComponent(jLabelLimite)
                    .addGroup(jPanelEmprestimoLayout.createSequentialGroup()
                        .addComponent(jLabelEmp)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldVal, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        jPanelEmprestimoLayout.setVerticalGroup(
            jPanelEmprestimoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelEmprestimoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelSaldo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelLimite)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelEmprestimoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelEmp)
                    .addComponent(jTextFieldVal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addComponent(jLabel1))
        );

        getContentPane().add(jPanelEmprestimo, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonActionPerformed
        
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            float result = Float.valueOf(jTextFieldVal.getText());
            if (pj == null) {
                if (result > 0) {
                    pf.devolver(result);
                } else {
                    pf.emprestar(result);
                }
                PessoaFisicaDAO.save(session, pf);
            } else {
                if (result > 0) {
                    pj.devolver(result);
                } else {
                    pj.emprestar(result);
                }
                PessoaJuridicaDAO.save(session, pj);
            } 
            session.getTransaction().commit();
            JOptionPane.showMessageDialog(this, "Operação realizada com sucesso");
            this.dispose();            
        } catch (Exception e) {
            
        }
        
        // TODO add your handling code here:
    }//GEN-LAST:event_jButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelEmp;
    private javax.swing.JLabel jLabelLimite;
    private javax.swing.JLabel jLabelSaldo;
    private javax.swing.JPanel jPanelEmprestimo;
    private javax.swing.JTextField jTextFieldVal;
    // End of variables declaration//GEN-END:variables
}
